package com.hnevc.foodserver.controller;

import com.hnevc.foodserver.bean.Category;
import com.hnevc.foodserver.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/category")
@Api(tags = "新闻类别接口")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    @PostMapping("/list")
    @ApiOperation(value = "获取类别信息", notes = "获取类别信息，返回所有的类别信息")

    public Map<String, Object> list() {
        Map<String, Object> map = new HashMap<>();
        try {
            List<Category> all = categoryService.list();
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", all);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }

    @PostMapping("/findone")
    @ApiOperation(value = "获取某一记录", notes = "根据样例，获取某一记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Category", value = "类别信息")
    })
    public Map<String, Object> findone(@RequestBody Category Category) {
        Map<String, Object> map = new HashMap<>();
        try {
            Category category1 = categoryService.findone(Category);
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", category1);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", e.getMessage());
        }
        return map;
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新类别信息", notes = "更新类别信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Category", value = "类别信息")
    })

    public Map<String, Object> update(@RequestBody Category Category) {
        Map<String, Object> map = new HashMap<>();
        try {
            Category category1 = categoryService.update(Category);
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", category1);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", e.getMessage());
        }
        return map;
    }

    @PostMapping("/deleteone/{id}")
    @ResponseBody
    @ApiOperation(value = "删除某一记录", notes = "根据id，删除某一记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Category", value = "类别信息")
    })

    public Map<String, Object> delete(@PathVariable("id") Integer id) {
        Map<String, Object> map = new HashMap<>();
        try {
            categoryService.deleteById(id);
            map.put("success", true);
            map.put("msg", "成功");
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", e.getMessage());
        }
        return map;
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加某一记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Category", value = "类别信息")
    })
    public Map<String, Object> add(@RequestBody Category Category) {
        Map<String, Object> map = new HashMap<>();

        Category category1 = categoryService.add(Category);
        if (category1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", category1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }

        return map;
    }
}
